//enum Color { RED, BLACK };
//
//template<class K, class V>
//struct RBTreeNode
//{
//	RBTreeNode<K, V>* _left;
//	RBTreeNode<K, V>* _right;
//	RBTreeNode<K, V>* _parent;
//
//	pair<K, V> _kv;
//	Color _col;
//
//	RBTreeNode(const pair<K, V>& kv)
//		:_left(nullptr)
//		,_right(nullptr)
//		,_parent(nullptr)
//		,_kv(kv)
//		,_col(RED)
//	{}
//};
//
//template<class K, class V>
//class RBTree
//{
//	typedef RBTreeNode<K, V> Node;
//public:
//
//	bool insert(const pair<K, V>& kv)
//	{
//		if (_root == nullptr)
//		{
//			_root = new Node(kv);
//			_root->_col = BLACK;
//			return true;
//		}
//
//		Node* cur = _root;
//		Node* parent = nullptr;
//
//		while (cur)
//		{
//			if (cur->_kv.first < kv.first)
//			{
//				parent = cur;
//				cur = cur->_right;
//			}
//			else if (cur->_kv.first > kv.first)
//			{
//				parent = cur;
//				cur = cur->_left;
//			}
//			else
//			{
//				return false;
//			}
//		}
//
//		cur = new Node(kv);
//		if (parent->_kv.first > kv.first)
//			parent->_left = cur;
//		else
//			parent->_right = cur;
//		cur->_col = RED;
//		cur->_parent = parent;
//
//		while (parent && parent->_col == RED)
//		{
//			Node* grandfather = parent->_parent;
//			assert(grandfather && grandfather->_col == BLACK);
//
//			if (parent == grandfather->_left)
//			{
//				Node* uncle = grandfather->_right;
//
//				if (uncle && uncle->_col == RED)
//				{
//					parent->_col = uncle->_col = BLACK;
//					grandfather->_col = RED;
//					cur = grandfather;
//					parent = cur->_parent;
//				}
//				else
//				{
//					if (cur == parent->_left)
//					{
//						RotateR(grandfather);
//						parent->_col = BLACK;
//						grandfather->_col = RED;
//					}
//					else
//					{
//						RotateL(parent);
//						RotateR(grandfather);
//						cur->_col = BLACK;
//						grandfather->_col = RED;
//					}
//
//					break;
//				}
//			}
//			else
//			{
//				Node* uncle = grandfather->_left;
//
//				if (uncle && uncle->_col == RED)
//				{
//					parent->_col = uncle->_col = BLACK;
//					grandfather->_col = RED;
//					cur = grandfather;
//					parent = cur->_parent;
//				}
//				else
//				{
//					if (cur == parent->_right)
//					{
//						RotateL(grandfather);
//						parent->_col = BLACK;
//						grandfather->_col = RED;
//					}
//					else
//					{
//						RotateR(parent);
//						RotateL(grandfather);
//						cur->_col = BLACK;
//						grandfather->_col = RED;
//					}
//
//					break;
//				}
//			}
//		}
//
//		_root->_col = BLACK;
//		return true;
//	}
//
//	bool IsRBTree()
//	{
//		if (_root == nullptr)
//			return true;
//
//		if (_root->_col != BLACK)
//			return false;
//
//		int benchmake = 0;
//		return _IsRBTree(_root, 0, benchmake);
//	}
//	void InOrder() const { _InOrder(_root); }
//
//private:
//
//	void RotateR(Node* parent)
//	{
//		Node* cur = parent->_left;
//		Node* curright = cur->_right;
//		Node* ppnode = parent->_parent;
//
//		parent->_left = curright;
//		if (curright)
//			curright->_parent = parent;
//
//		cur->_right = parent;
//		parent->_parent = cur;
//
//		if (ppnode == nullptr)
//		{
//			_root = cur;
//			cur->_parent = nullptr;
//		}
//		else
//		{
//			if (ppnode->_left == parent)
//				ppnode->_left = cur;
//			else
//				ppnode->_right = cur;
//			cur->_parent = ppnode;
//		}
//	}
//
//	void RotateL(Node* parent)
//	{
//		Node* cur = parent->_right;
//		Node* curleft = cur->_left;
//		Node* ppnode = parent->_parent;
//
//		parent->_right = curleft;
//		if (curleft)
//			curleft->_parent = parent;  
//
//		cur->_left = parent;
//		parent->_parent = cur;
//
//		if (ppnode == nullptr)
//		{
//			_root = cur;
//			cur->_parent = nullptr;
//		}
//		else
//		{
//			if (ppnode->_left == parent)
//				ppnode->_left = cur;
//			else
//				ppnode->_right = cur;
//			cur->_parent = ppnode;
//		}
//	}
//
//	bool _IsRBTree(Node* root, int blacknum, int& benchmake)
//	{
//		if (root == nullptr)
//		{
//			if (!benchmake)
//				benchmake = blacknum;
//			if (blacknum != benchmake)
//				return false;
//			return true;
//		}
//
//		if (root->_col == BLACK)
//			blacknum++;
//
//		if (root->_col == RED && root->_parent->_col == RED)
//		{
//			cout << "连续出现两个红色结点~错误！" << endl;
//			return false;
//		}
//
//		return _IsRBTree(root->_left, blacknum, benchmake) &&
//			_IsRBTree(root->_right, blacknum, benchmake);
//	}
//
//	void _InOrder(Node* root) const 
//	{
//		if (root == nullptr)
//			return;
//
//		_InOrder(root->_left);
//		cout << root->_kv.first << ':' << root->_kv.second << endl;
//		_InOrder(root->_right);
//	}
//
//	Node* _root = nullptr;
//};


enum Color { RED, BLACK };

template<class T>
struct RBTreeNode
{
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;

	T _data;
	Color _col;

	RBTreeNode(const T& data)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _data(data)
		, _col(RED)
	{}
};

template<class T, class Ref, class Ptr>
struct __RBTree_Iterator
{
	typedef RBTreeNode<T> Node;
	typedef __RBTree_Iterator<T, Ref, Ptr> Self;

	Node* _node;

	__RBTree_Iterator(Node* node) :_node(node) {}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	Self& operator++()
	{
		if (_node->_right)
		{
			Node* rleft = _node->_right;
			while (rleft->_left)
				rleft = rleft->_left;
			_node = rleft;
		}
		else
		{
			Node* parent = _node->_parent;
			Node* cur = _node;
			while (parent && parent->_right == cur)
			{
				parent = parent->_parent;
				cur = cur->_parent;
			}
			_node = parent;
		}

		return *this;
	}

	Self operator++(int)
	{
		Self tmp(_node);
		++(*this);
		return tmp;
	}

	bool operator==(const Self& s) { return _node == s._node; }
	bool operator!=(const Self& s) { return _node != s._node; }
};

template<class K, class T, class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef __RBTree_Iterator<T, const T&, const T*> iterator;
	typedef __RBTree_Iterator<T, const T&, const T*> const_iterator;

	iterator begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
			cur = cur->_left;
		return iterator(cur);
	}
	iterator end() { return iterator(nullptr); }

	const_iterator begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
			cur = cur->_left;
		return iterator(cur);
	}
	const_iterator end() const { return const_iterator(nullptr); }

	pair<iterator, bool> insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return make_pair(iterator(_root), true);
		}

		KeyOfT kft;
		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (kft(cur->_data) < kft(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kft(cur->_data) > kft(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return make_pair(iterator(cur), false);
			}
		}

		cur = new Node(data);
		if (kft(parent->_data) > kft(data))
			parent->_left = cur;
		else
			parent->_right = cur;
		cur->_col = RED;
		cur->_parent = parent;
		Node* newnode = cur;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			assert(grandfather && grandfather->_col == BLACK);

			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;

				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;

				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break; 
				}
			}
		}

		_root->_col = BLACK;
		return make_pair(iterator(newnode), true);
	}

	iterator find(const T& key)
	{
		if (_root == nullptr) { return iterator(nullptr); }
		
		Node* cur = _root;
		KeyOfT kft;

		while (cur)
		{
			if (kft(cur->_data) > key)
				cur = cur->_left;
			else if (kft(cur->_data) < key)
				cur = cur->_right;
			else
				return iterator(cur);
		}

		return iterator(nullptr);
	}

	bool IsRBTree()
	{
		if (_root == nullptr)
			return true;

		if (_root->_col != BLACK)
			return false;

		int benchmake = 0;
		return _IsRBTree(_root, 0, benchmake);
	}
	void InOrder() const { _InOrder(_root); }

private:

	void RotateR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* curright = cur->_right;
		Node* ppnode = parent->_parent;

		parent->_left = curright;
		if (curright)
			curright->_parent = parent;

		cur->_right = parent;
		parent->_parent = cur;

		if (ppnode == nullptr)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (ppnode->_left == parent)
				ppnode->_left = cur;
			else
				ppnode->_right = cur;
			cur->_parent = ppnode;
		}
	}

	void RotateL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* curleft = cur->_left;
		Node* ppnode = parent->_parent;

		parent->_right = curleft;
		if (curleft)
			curleft->_parent = parent;

		cur->_left = parent;
		parent->_parent = cur;

		if (ppnode == nullptr)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (ppnode->_left == parent)
				ppnode->_left = cur;
			else
				ppnode->_right = cur;
			cur->_parent = ppnode;
		}
	}

	bool _IsRBTree(Node* root, int blacknum, int& benchmake)
	{
		if (root == nullptr)
		{
			if (!benchmake)
				benchmake = blacknum;
			if (blacknum != benchmake)
				return false;
			return true;
		}

		if (root->_col == BLACK)
			blacknum++;

		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "连续出现两个红色结点~错误！" << endl;
			return false;
		}

		return _IsRBTree(root->_left, blacknum, benchmake) &&
			_IsRBTree(root->_right, blacknum, benchmake);
	}

	void _InOrder(Node* root) const
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << ':' << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	Node* _root = nullptr;
};
